package com.leyou.item.controller;


import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entitiy.TbBrand;
import com.leyou.item.service.TbBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 品牌表，一个品牌下有多个商品（spu），一对多关系 前端控制器
 * </p>
 *
 * @author HM
 * @since 2021-06-16
 */
@Controller
@RequestMapping("/brand")
public class TbBrandController {
    @Autowired
    private TbBrandService brandService;
    /*asdadasd*/
    @GetMapping("/page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page", defaultValue = "1")Integer page,
            @RequestParam(value = "rows", defaultValue = "5")Integer rows,
            @RequestParam(value = "key", required = false)String key,
            @RequestParam(value = "sortBy", required = false)String sortBy,
            @RequestParam(value = "desc", defaultValue = "false")Boolean desc
    ){
        PageResult<BrandDTO> brandDTOPageResult = brandService.searchPage(page,rows, key, sortBy, desc);
        return ResponseEntity.ok(brandDTOPageResult);
    }

    /**
     * 新增品牌
     * @param brand
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> save(TbBrand brand, @RequestParam(name = "cids") List<Long> cids){
        brandService.saveBrandAndCategory(brand,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    /**
     * 修改品牌
     * @param brand
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> update(TbBrand brand,@RequestParam(name = "cids") List<Long> cids){
        brandService.updateBrandAndCategory(brand,cids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据分类ID查询品牌列表
     * @param categoryId 商品分类id
     * @return 该分类下的品牌的集合
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryByCategoryId(@RequestParam("id") Long categoryId) {
        List<BrandDTO> list = this.brandService.queryByCategoryId(categoryId);
        return ResponseEntity.ok(list);
    }
}

